<?php

/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin_model
 *
 * @author hassanali
 */
class admin_model extends APP_Model {

    //put your code here
    public $session_admin_data;
    public $admin_id;

    /**
     * 
     */
    function __construct() {
        parent::__construct();

        $this->session_admin_data = array(
            'admin_id' => $this->session->userdata('admin_ID'),
            'admin_name' => $this->session->userdata('admin_NAME'),
            'admin_email' => $this->session->userdata('admin_EMAIL'),
            'admin_file' => $this->session->userdata('admin_FILE'),
            'admin_password' => $this->session->userdata('admin_PASSWORD'),
            'admin_lastlogin' => $this->session->userdata('admin_LASTLOGIN'),
            'admin_mode' => $this->session->userdata('admin_MODE'),
            'admin_controal' => $this->session->userdata('admin_CONTROAL'),
            'admin_logged_in' => $this->session->userdata('admin_logged_in')
        );
        $this->admin_id = $this->session->userdata('admin_ID');
    }

    /**
     * 
     * @param type $table
     * @param type $where
     * @param type $url
     * @param type $messgae
     * @return type
     */

    /**
     * 
     * @param array $data
     */
    function add_manager_report($data = array()) {

        if (is_array($data['contant'])) {
            $returndata = array();
            foreach ($data['contant'] as $key => $value) {
                $returndata[$key] = $value;
            }
            $data['contant'] = json_encode($returndata);
        }

        if (empty($data['user_id'])) {
            $data['user_id'] = $this->admin_id;
        }

        $data['create_date'] = date('Y-m-d H:i:s');
        $this->db->insert('manager_reports', $data);
    }

    function filter($count = FALSE, $offset = false, $trash = false, $where = false) {

        $data = $this->input->get();
        $fields = $this->db->list_fields($this->table);

        if ($this->where) {
            $this->db->where($this->where);
        }
        if($where){
            $this->db->where($where);
        }


        if ($data) {
            foreach ($this->columns_op as $condition => $function) {
                $conditiont = explode(' ', $condition);
                $index = 0;
                $remove = false;
                foreach ($data as $key => $value) {
                    $key = explode('-', $key);
                    if (count($key) > 1) {
                        if ($key[1] == $conditiont[0]) {
                            if ($value) {
                                $function = end(explode('Callback_', $function));
                                $value = $function($value);
                                if (in_array($key[1], $fields))
                                    $this->db->{$this->search_types[$key[0]]}($this->table . '.' . $condition, $value);
                                else
                                    $this->db->{$this->search_types[$key[0]]}($condition, $value);
                            }
                            $remove = implode('-', $key);
                            break;
                        }
                    }
                }
                if ($remove) {
                    unset($data[$remove]);
                    $remove = false;
                }
            }
            foreach ($data as $key => $value) {
                if ($value) {
                    $key = explode('-', $key);
                    if (count($key) > 1) {
                        if (in_array($key[1], $fields))
                            $this->db->{$this->search_types[$key[0]]}($this->table . '.' . $key[1], $value);
                        else
                            $this->db->{$this->search_types[$key[0]]}($key[1], $value);
                    }
                }
            }
        }



        if (count($this->select)) {
            $this->db->select($this->select);
        }
        if (count($this->order)) {
            $this->orderby($this->order);
        }
        if (count($this->join)) {
            $this->join($this->join);
        }
        if (!$count) {
            if (!$data) {
                $this->db->limit(ADMIN_LIMIT, $offset);
            } else {
                if (isset($data['dblimit'])) {
                    if ($data['dblimit']) {
                        $this->db->limit($data['dblimit']);
                    } else {
                        $this->db->limit(ADMIN_LIMIT, $offset);
                    }
                } else {
                    $this->db->limit(ADMIN_LIMIT, $offset);
                }
            }
            $this->db->offset($offset);
        }


        // trash 
        if (in_array('trash', $fields)) {
            if ($trash) {
                $this->db->where($this->table . '.trash', 'YES');
            } else {
                $this->db->where($this->table . '.trash', 'NO');
            }
        }


        $sql = $this->db->get($this->table);
        if ($count) {
            return $sql->num_rows();
        } else {
            return $sql->result();
        }
    }

}

?>
